﻿var tablepath = GetRootPath()+"Resource/component/table/template.html";
Vue.component('common-table', function (resolve, reject) {
    $.get(tablepath).then(function (res) {
        resolve({
            template: res,
            props: {
                data: {
                  type: Array,
                  default:()=>[]
                },
                showCheckBox:{
                    type: Boolean,
                    default:true
                },
                showIndex:{
                    type: Boolean,
                    default:true
                },
                showEdit:{
                    type: Boolean,
                    default:true
                },
                showDelete:{
                    type: Boolean,
                    default:true
                },
                pageIndex:{
                    type: Number,
                    default:1
                },
                pageSize:{
                    type: Number,
                    default:10
                },
                total:{
                    type: Number,
                    default:0
                },
                tableHeight:{
                    type: Number,
                    default:500
                }

            },
            data() {
                return { 
                    tableSelect: [],
                    currentPage:1
                 };
            },
            watch: {
                pageIndex(val){
                    this.currentPage = val
                }
            },
            mounted:function(){
                this.$emit("on-load");
            },
            methods: {
                handleSizeChange(val) {

                },
                handleCurrentChange(val) {
                  this.$emit("on-pageindexchange",val);
                  this.$emit("on-load");
                },
                handleEdit(index, row) {
                  this.$emit("handle-edit", index, row);
                },
                handleDelete(index, row) {
                  this.$emit("handle-delete", index, row);
                },
                indexMethod(index) {
                  return (this.pageIndex - 1) * this.pageSize + index + 1;
                },
                selectionChange(val) {
                  this.tableSelect = val;
                },
                getSelectRow() {
                  return this.tableSelect;
                }
            }
        })
    });
});

function GetRootPath() {
    var loc = window.location,
        host = loc.hostname,
        protocol = loc.protocol,
        port = loc.port ? (':' + loc.port) : '';
    var path = location.pathname;

    if (path.indexOf('/') === 0) {
        path = path.substring(1);
    }

    var mypath = '/' + path.split('/')[0];
    path = (mypath != undefined ? mypath : ('/' + loc.pathname.split('/')[1])) + '/';

    var rootPath = protocol + '//' + host + port + path;
    return rootPath;
}

function uuid() {
    var date = new Date();
    return dateToString(date) + 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}
